﻿using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Might.Infrastructure.Annotations.Graphics;
using Might.Infrastructure.DOM;
using Might.Infrastructure.ModelicaDOM;

namespace Might.Modelica.Annotations.Graphics {
    ///<summary>
    ///</summary>
    [Mapping("Icon")]
    [AnnotationDOMGroup(GroupNames.Icons)]
    public class Icon : GraphicsItem {
        private readonly ObservableCollection<IGraphicsItem> children;

        ///<summary>
        ///</summary>
        public Icon() : this(new CoordinateSystem(), new ObservableCollection<IGraphicsItem>()) {}

        ///<summary>
        ///</summary>
        ///<param name="coordinateSystem"></param>
        ///<param name="graphics"></param>
        public Icon(CoordinateSystem coordinateSystem, ObservableCollection<IGraphicsItem> graphics) {
            this.coordinateSystem = coordinateSystem;
            children = graphics;
            children.CollectionChanged += children_CollectionChanged;
        }
        
        #region Specific Modelica members

        ///<summary>
        ///</summary>
        [Mapping("coordinateSystem")]
        public CoordinateSystem coordinateSystem { get; set; }

        ///<summary>
        ///</summary>
        [Mapping("graphics"), DefaultMapping]
        public ObservableCollection<IGraphicsItem> Children {
            get { return children; }
        }

        private void children_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
            if (e.Action == NotifyCollectionChangedAction.Add) {
                foreach (IDomNode node in e.NewItems) {
                    node.Parent = this;
                }
            }

            OnChanged(this, "Children");
        }

        public override bool IsDefault {
            get { return coordinateSystem.IsDefault && Children.Count == 0; }
        }

        #endregion
    }
}